﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using StairsModuleAndEdit.StairsEdit.Common;

namespace StairsModuleAndEdit.StairsEdit.Managers
{
	internal class HideOrShowStairsCls
	{
		public HideOrShowStairsCls(ExternalCommandData commandData)
		{
			this.m_commandData = commandData;
			this.m_UiDoc = commandData.Application.ActiveUIDocument;
			this.m_Doc = this.m_UiDoc.Document;
		}

		public Result HideOrShowStairsKit(ref string Message)
		{
			JudgeHidingCls judgeHidingCls = new JudgeHidingCls(this.m_commandData);
			List<ElementId> list = null;
			List<ElementId> list2 = null;
			judgeHidingCls.FindHidingStairs(out list, out list2);
			if (list.Count > 0 || list2.Count > 0)
			{
				this.ShowHidingStairs(list, list2, ref Message);
				MessageBox.Show("完成显示面层", "盈建科提示");
			}
			else
			{
				judgeHidingCls.FindneedHideStairs(out list, out list2);
				if (list.Count > 0 || list2.Count > 0)
				{
					this.HideArchStairs(list, list2, ref Message);
					MessageBox.Show("完成隐藏面层", "盈建科提示");
				}
				else
				{
					MessageBox.Show("不需要进行隐藏或者显示面层", "盈建科提示");
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private bool ShowHidingStairs(List<ElementId> HideStairs, List<ElementId> EditStairs, ref string Message)
		{
			Transaction transaction = new Transaction(this.m_Doc, "显示面层");
			transaction.Start();
			try
			{
				if (HideStairs.Count > 0)
				{
					this.m_Doc.ActiveView.UnhideElements(HideStairs);
				}
				foreach (ElementId elementId in EditStairs)
				{
					Stairs stairs = this.m_Doc.GetElement(elementId) as Stairs;
					StairsType stairsType = this.m_Doc.GetElement(stairs.GetTypeId()) as StairsType;
					StairsRunType stairsRunType = this.m_Doc.GetElement(stairsType.RunType) as StairsRunType;
					stairsRunType.HasRisers = true;
					stairsRunType.HasTreads = true;
					double treadThickness = stairsRunType.TreadThickness;
					Parameter parameter = stairs.GetParameter(BuiltInParameter.STAIRS_BASE_OFFSET);
					Parameter parameter2 = stairs.GetParameter(BuiltInParameter.STAIRS_TOP_OFFSET);
					double num = parameter.AsDouble();
					double num2 = parameter2.AsDouble();
					parameter.Set(num + treadThickness);
					if (!parameter2.IsReadOnly)
					{
						parameter2.Set(num2 + treadThickness);
					}
					List<ElementId> list = stairs.GetStairsRuns() as List<ElementId>;
					foreach (ElementId elementId2 in list)
					{
						StairsRun elem = this.m_Doc.GetElement(elementId2) as StairsRun;
						Parameter parameter3 = elem.GetParameter("延伸到踢面底部之下");
						if (parameter3 != null)
						{
							parameter3.Set(parameter3.AsDouble() - treadThickness);
							break;
						}
					}
					StairsXDataManager.DeleteArchStairsEntity(stairs);
				}
				transaction.Commit();
			}
			catch (Exception ex)
			{
				transaction.RollBack();
				Message = ex.Message;
			}
			return true;
		}

		private bool HideArchStairs(List<ElementId> HideStairs, List<ElementId> EditStairs, ref string Message)
		{
			Transaction transaction = new Transaction(this.m_Doc, "隐藏面层");
			transaction.Start();
			try
			{
				if (HideStairs.Count > 0)
				{
					this.m_Doc.ActiveView.HideElements(HideStairs);
				}
				Dictionary<ElementId, double> dictionary = new Dictionary<ElementId, double>();
				foreach (ElementId elementId in EditStairs)
				{
					Stairs stairs = this.m_Doc.GetElement(elementId) as Stairs;
					StairsType stairsType = this.m_Doc.GetElement(stairs.GetTypeId()) as StairsType;
					StairsRunType stairsRunType = this.m_Doc.GetElement(stairsType.RunType) as StairsRunType;
					double num = stairsRunType.TreadThickness;
					if (dictionary.ContainsKey(stairsRunType.Id))
					{
						num = dictionary[stairsRunType.Id];
					}
					else
					{
						dictionary.Add(stairsRunType.Id, num);
					}
					stairsRunType.HasRisers = false;
					stairsRunType.HasTreads = false;
					Parameter parameter = stairs.GetParameter(BuiltInParameter.STAIRS_BASE_OFFSET);
					Parameter parameter2 = stairs.GetParameter(BuiltInParameter.STAIRS_TOP_OFFSET);
					double num2 = parameter.AsDouble();
					double num3 = parameter2.AsDouble();
					parameter.Set(num2 - num);
					if (!parameter2.IsReadOnly)
					{
						parameter2.Set(num3 - num);
					}
					List<ElementId> list = stairs.GetStairsRuns() as List<ElementId>;
					foreach (ElementId elementId2 in list)
					{
						StairsRun elem = this.m_Doc.GetElement(elementId2) as StairsRun;
						Parameter parameter3 = elem.GetParameter("延伸到踢面底部之下");
						if (parameter3 != null)
						{
							parameter3.Set(parameter3.AsDouble() + num);
							break;
						}
					}
					StairsXDataManager.WriteArchStairsId(stairs, ElementId.InvalidElementId);
				}
				transaction.Commit();
			}
			catch (Exception ex)
			{
				Message = ex.Message;
				transaction.RollBack();
				return false;
			}
			return true;
		}

		private ExternalCommandData m_commandData;

		private UIDocument m_UiDoc;

		private Document m_Doc;
	}
}
